package com.wh.wisdomsite.xbox.server.controller.pr;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wh.wisdomsite.xbox.ba.entity.CommonDict;
import com.wh.wisdomsite.xbox.ba.entity.Menu;
import com.wh.wisdomsite.xbox.ba.service.ICommonDictService;
import com.wh.wisdomsite.xbox.common.util.ResManager;
import com.wh.wisdomsite.xbox.pojo.pr.ProjectIn;
import com.wh.wisdomsite.xbox.pr.entity.Project;
import com.wh.wisdomsite.xbox.pr.service.IProjectService;
import com.wh.wisdomsite.xbox.server.util.MenuUtils;
import com.wh.wisdomsite.xbox.server.util.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author 蔡道行
 * @version V1.0
 * @date 11:42 2018/11/20
 * @Copyright 公司版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2014
 */
@Controller
@RequestMapping(value = "/projectManagement")
public class ProjectManagementController {

    private static final Log logger = LogFactory.getLog(ProjectController.class);

    @Autowired
    private IProjectService iProjectService;

    @Autowired
    private ICommonDictService iCommonDictService;

    /**
     * 页面跳转
     *
     * @return
     */
    @RequiresPermissions("rules:pr_projectManagement_page")
    @RequestMapping(value = "index.do", method = RequestMethod.GET)
    public String index(String prTypeFromJump,HttpServletRequest request) {
        String prTypeId = "";
        //首页点击图标跳转后查找项目类型
        if(StringUtils.isNotEmpty(prTypeFromJump)){
            EntityWrapper<CommonDict> ewCommonDict = new EntityWrapper<CommonDict>();
            ewCommonDict.setEntity(new CommonDict());
            //房建
            if(prTypeFromJump.equals("1")){
                ewCommonDict.eq("propertyName","房建");
            }
            //市政
            if(prTypeFromJump.equals("2")){
                ewCommonDict.eq("propertyName","市政");
            }
            List<CommonDict> commonDicts = iCommonDictService.selectList(ewCommonDict);
            if(commonDicts.size() > 0){
                prTypeId = commonDicts.get(0).getCdId();
            }
        }
        request.setAttribute("prTypeFromJump",prTypeId);
        return "/pr/projectManagement/projectManagementList";
    }

    /**
     * 获取项目集合
     *
     * @param response
     * @param in
     */
    @RequiresPermissions("rules:pr_projectManagement_find")
    @RequestMapping(value = "/projectList.do")
    public void projectList(HttpServletResponse response, ProjectIn in) {
        try {
            logger.info("加载项目数据列表-->>开始");
            //设置分页
            if (in.getPage() == 0) in.setPage(1);
            if (in.getRows() == 0) in.setRows(10);
            Page<Project> page = new Page<Project>();
            page.setCurrent(in.getPage());
            page.setSize(in.getRows());
            //设置条件
            EntityWrapper<Project> ewProject = new EntityWrapper<Project>();
            ewProject.setEntity(new Project());
            if(StringUtils.isNotEmpty(in.getPrName())) {
                ewProject.like("p.prName", in.getPrName());
            }
            if(StringUtils.isNotEmpty(in.getPrType())) {
                ewProject.eq("p.prType", in.getPrType());
            }else{
                if(StringUtils.isNotEmpty(in.getPrTypeFromJump())) {
                    ewProject.eq("p.prType", in.getPrTypeFromJump());
                }
            }
            if(StringUtils.isNotEmpty(in.getPrCode())) {
                ewProject.eq("p.prCode", in.getPrCode());
            }
            if(StringUtils.isNotEmpty(in.getConstructType())) {
                ewProject.like("p.constructType", in.getConstructType());
            }
            if(StringUtils.isNotEmpty(in.getPrPurpose())) {
                ewProject.eq("p.prPurpose", in.getPrPurpose());
            }
            //若用户不是超级管理员根据用户的数据权限添加查询条件
            if(!UserUtils.isSysAdmin()){
                ewProject = UserUtils.setEntityWrapper(ewProject, "c1", "o1", "p");
            }
            ewProject.orderBy("createDate", true);
            Page<Map<String,Object>> projectPage = iProjectService.getMapsPage(page,ewProject);
            JSONObject json = new JSONObject();
            JSONArray array = new JSONArray();
            if(projectPage.getRecords().size() > 0){
                for (Map<String,Object> project : projectPage.getRecords()) {
                    JSONObject object = new JSONObject(project);
                    object.put("id", project.get("prId"));
                    if(project.get("prType")!= null){
                        object.put("prType",new JSONObject(project.get("prType")));
                    }
                    if(project.get("prProvice")!=null){
                        object.put("prProvice",new JSONObject(project.get("prProvice")));
                    }
                    if(project.get("prCity")!=null){
                        object.put("prCity",new JSONObject(project.get("prCity")));
                    }
                    if(project.get("prDistrict")!=null){
                        object.put("prDistrict",new JSONObject(project.get("prDistrict")));
                    }
                    if(project.get("prApprovalLevel")!=null){
                        object.put("prApprovalLevel",new JSONObject(project.get("prApprovalLevel")));
                    }
                    if(project.get("constructType")!=null){
                        object.put("constructType",new JSONObject(project.get("constructType")));
                    }
                    if(project.get("prPurpose")!=null){
                        object.put("prPurpose",new JSONObject(project.get("prPurpose")));
                    }
                    if(project.get("bidType")!=null){
                        object.put("bidType",new JSONObject(project.get("bidType")));
                    }
                    if(project.get("company")!=null){
                        object.put("company",new JSONObject(project.get("company")));
                    }
                    if(project.get("office")!=null){
                        object.put("office",new JSONObject(project.get("office")));
                    }
                    array.put(object);
                }
            }
            json.put("total", projectPage.getTotal());
            json.put("rows", array);
            ResManager.getTextOut(response).print(json);
            logger.info("加载项目数据列表-->>结束");
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug("加载项目数据列表-->>Exception" + e.getMessage());
        }
    }

    /**
     * 项目查看
     * @param projectIn
     * @param request
     * @return
     */
    @RequiresPermissions("rules:pr_projectManagement_check")
    @RequestMapping("/projectView.do")
    public String projectView( ProjectIn projectIn, HttpServletRequest request) {
        logger.info("项目查看-->>开始");
        Menu menu = new Menu();
        try {
            menu = MenuUtils.getMenuByPermissionInTreeMenus(
                    "rules:pr_projectManagement_check", UserUtils.getServerSessionUser().getMenuList());
        }catch (Exception e){
            e.printStackTrace();
        }
        request.setAttribute("prId", projectIn.getPrId());
        if(menu != null){
            JSONArray array = new JSONArray();
            if(menu.getChildren().size() > 0){
                for(Menu menu1 : menu.getChildren()){
                    JSONObject object = new JSONObject(menu1);
                    array.put(object);
                }
            }
            request.setAttribute("menus", array);
        }
        logger.info("项目查看-->>结束");
        return "/pr/projectManagement/projectView";
    }
}

